/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * StaffManagement.java
 *
 * Created on Mar 27, 2011, 10:30:39 PM
 */
package vn.aptech.project.view;

import vn.aptech.project.control.Employee_Detail_BLL;
import vn.aptech.project.control.tblorder_BLL;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Mr.Hiep
 */
public class StaffManagement extends javax.swing.JPanel {

    /** Creates new form StaffManagement */
    public StaffManagement() throws IOException, SQLException, ClassNotFoundException {
        initComponents();
        SelectEmployee_details();
    }

    public void SelectEmployee_details() throws IOException, SQLException, ClassNotFoundException {
        Employee_Detail_BLL Edb = new Employee_Detail_BLL();
        Vector vt;
        if (!txtStaffName.getText().equals("") && !txtStaffName.getText().equals("Enter staff name")) {
            vt = Edb.SearchEmployee_DetailByName(txtStaffName.getText());
        } else {
            vt = Edb.selectEmployee_Detail();
        }
        Vector vtCol = new Vector();
        vtCol.add("Id");
        vtCol.add("Use name");
        vtCol.add("Address");
        vtCol.add("Contact_no");
        vtCol.add("Password");
        vtCol.add("Department");
        DefaultTableModel dtm = new DefaultTableModel(vt, vtCol) {

            @Override
            public boolean isCellEditable(int rowIndex, int mColIndex) {
                return false;
            }
        };
        tblEmp.setModel(dtm);


    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        tblEmp = new javax.swing.JTable();
        txtStaffName = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        bntCreate = new javax.swing.JButton();
        bntEdit = new javax.swing.JButton();
        bntDelete = new javax.swing.JButton();

        setBackground(new java.awt.Color(255, 255, 255));
        setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jScrollPane1.setBackground(new java.awt.Color(255, 255, 255));

        tblEmp.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(tblEmp);

        add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 540, 355));

        txtStaffName.setText("Enter staff name");
        add(txtStaffName, new org.netbeans.lib.awtextra.AbsoluteConstraints(150, 370, 250, -1));

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/Search.png"))); // NOI18N
        jButton1.setText("Search");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        add(jButton1, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 410, 100, -1));

        bntCreate.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/add_user.png"))); // NOI18N
        bntCreate.setText("Ceate New Staff");
        bntCreate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bntCreateActionPerformed(evt);
            }
        });
        add(bntCreate, new org.netbeans.lib.awtextra.AbsoluteConstraints(150, 410, 140, -1));

        bntEdit.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/edit.png"))); // NOI18N
        bntEdit.setText("Edit Staff");
        bntEdit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bntEditActionPerformed(evt);
            }
        });
        add(bntEdit, new org.netbeans.lib.awtextra.AbsoluteConstraints(300, 410, 100, -1));

        bntDelete.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/delete.png"))); // NOI18N
        bntDelete.setText("Delete Staff");
        bntDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bntDeleteActionPerformed(evt);
            }
        });
        add(bntDelete, new org.netbeans.lib.awtextra.AbsoluteConstraints(413, 410, 110, -1));
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
          try {
            try {
                SelectEmployee_details();
            } catch (SQLException ex) {
                Logger.getLogger(StaffManagement.class.getName()).log(Level.SEVERE, null, ex);
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(StaffManagement.class.getName()).log(Level.SEVERE, null, ex);
            }
        } catch (IOException ex) {
            Logger.getLogger(Admin.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void bntCreateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bntCreateActionPerformed
        try {
            CreateStaff cs = new CreateStaff(null, true);
            cs.show();
            SelectEmployee_details();
        } catch (SQLException ex) {
            Logger.getLogger(StaffManagement.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(StaffManagement.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Admin.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_bntCreateActionPerformed

    private void bntEditActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bntEditActionPerformed
        int currentline = tblEmp.getSelectedRow();
        if (currentline < 0) {
            JOptionPane.showMessageDialog(this, "Select a line");
        } else {
            try {
                variable.Staffid = Integer.parseInt(tblEmp.getValueAt(currentline, 0).toString());
                variable.StaffName = tblEmp.getValueAt(currentline, 1).toString();
                variable.StaffAddr = tblEmp.getValueAt(currentline, 2).toString();
                variable.StaffContact = Integer.parseInt(tblEmp.getValueAt(currentline, 3).toString());
                variable.StaffPass = tblEmp.getValueAt(currentline, 4).toString();
                variable.StaffDepart = tblEmp.getValueAt(currentline, 5).toString();
                EditStaff es = new EditStaff(null, true);
                es.setVisible(true);
                SelectEmployee_details();
            } catch (SQLException ex) {
                Logger.getLogger(StaffManagement.class.getName()).log(Level.SEVERE, null, ex);
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(StaffManagement.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(Admin.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }//GEN-LAST:event_bntEditActionPerformed

    private void bntDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bntDeleteActionPerformed
         int reply = JOptionPane.showConfirmDialog(this, "Sure you delete", null, JOptionPane.OK_CANCEL_OPTION);
        if (reply == JOptionPane.YES_OPTION) {
            try {
                int currentline = tblEmp.getSelectedRow();
                if (currentline < 0) {
                    JOptionPane.showMessageDialog(this, "Select a line");
                } else {
                    tblorder_BLL tob = new tblorder_BLL();
                    Vector vt = tob.SearchByEmpName(String.valueOf(tblEmp.getValueAt(currentline, 1)));
                    if (vt.isEmpty()) {
                        int a = Integer.parseInt(String.valueOf(tblEmp.getValueAt(currentline, 0)));
                        Employee_Detail_BLL edb = new Employee_Detail_BLL();
                        edb.deleteEmployee_Detail(a);
                        SelectEmployee_details();
                        JOptionPane.showMessageDialog(this, "Successful Delete");
                    } else {
                        JOptionPane.showMessageDialog(this, "Cannot delete as products still recorded in bills");
                    }
                }
            } catch (IOException ex) {
                Logger.getLogger(Admin.class.getName()).log(Level.SEVERE, null, ex);
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(Admin.class.getName()).log(Level.SEVERE, null, ex);
            } catch (SQLException ex) {
                Logger.getLogger(Admin.class.getName()).log(Level.SEVERE, null, ex);
            }
    }
    }//GEN-LAST:event_bntDeleteActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bntCreate;
    private javax.swing.JButton bntDelete;
    private javax.swing.JButton bntEdit;
    private javax.swing.JButton jButton1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tblEmp;
    private javax.swing.JTextField txtStaffName;
    // End of variables declaration//GEN-END:variables
}
